package fr.univnantes.lina.uima.connectors.csv.ae;


import java.util.regex.Matcher;
import java.util.regex.Pattern;



import org.apache.uima.UimaContext;
import org.apache.uima.analysis_component.JCasAnnotator_ImplBase;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.Level;


import fr.univnantes.lina.uima.types.Grammaire;


public class CSV2CASAE extends JCasAnnotator_ImplBase {



private String[] mNature;
private Pattern[] mPatterns;



public void initialize(UimaContext aContext) throws ResourceInitializationException {


    super.initialize(aContext);
    // Get config. parameter values
    String[] patternStrings = (String[]) aContext.getConfigParameterValue("Patterns");
    mNature = (String[]) aContext.getConfigParameterValue("Nature");

    // compile regular expressions
    mPatterns = new Pattern[patternStrings.length];
    for (int i = 0; i < patternStrings.length; i++) {
      mPatterns[i] = Pattern.compile(patternStrings[i]);
    }
  }


@Override
public void process(JCas aJCas) throws AnalysisEngineProcessException {


String docText = aJCas.getDocumentText();

// loop over patterns
for (int i = 0; i < mPatterns.length; i++) {
Matcher matcher = mPatterns[i].matcher(docText);
while (matcher.find()) {
// found one - create annotation
Grammaire annotation = new Grammaire(aJCas);
annotation.setBegin(matcher.start());
annotation.setEnd(matcher.end());
annotation.addToIndexes();
annotation.setNature(mNature[i]);
getContext().getLogger().log(Level.FINEST, "Found: " + annotation);
System.out.println(matcher.start() + " " + matcher.end() + " " + mNature[i] + " " + annotation.getNature());
}
}


}



} 